/**
 * Created by GYL on 2018/7/25
 */
import React, { Component } from 'react';
import MyModal from '../../../components/common/MyModal';
import { Form, Row, Col, Input, message, Checkbox, DatePicker, Upload, Button, Icon, Tag } from 'antd';
import PublicService from '../../../services/PublicService';
import MyTable from '../../../components/common/MyTable';
import config from "../../../config";
import style from "./AddModal.less";
import moment from 'moment';
import request from '../../../utils/request';
const FormItem = Form.Item;
var tableArr

class CheckModel extends Component {
    state = {
        dataSource: [],
        columns: [
            {
                title: '序号',
                dataIndex: 'num',
                width: '5%',
                align: "center",
            },
            {
                title: '文件名',
                dataIndex: 'fileName',
                width: '20%',
                align: "center",
            },
            {
                title: "文件类型",
                dataIndex: 'fileType',
                width: '15%',
                align: "center",
            },
            {
                title: '文件大小',
                width: '10%',
                dataIndex: 'fileSize',
                align: "center",
            },
            {
                title: '上传时间',
                width: '20%',
                dataIndex: 'createTime',
                align: "center",
            },
            {
                title: '上传人',
                width: '15%',
                dataIndex: 'uploadUser',
                align: "center",
            },
            {
                title: '操作',
                width: '10%',
                dataIndex: 'uploadUs123er',
                align: "center",
                render: (text, record) => {
                    return <span>
                        <Tag color="#108ee9" onClick={this.onDelete.bind(this, record)}>删除</Tag>
                    </span>

                }
            },
        ]
    };
    componentDidMount() {
        let t = this;
        console.log(this.props.fileUploadList)
        // if(this.props.fileUploadList.length > 0) {
        //     this.setState({
        //         dataSource:this.props.fileUploadList
        //     })
        // }
        this.onsaveSc()



    };

        //删除上传文件
        onDelete = (record) => {
            console.log(record)
            request({
                url: `/zhrq-web/common/fileupload/deleteFile?id=${record.id}`,
                method: 'GET',
            }).then(data => {
                if (data.rc == 0) {
                    message.success('删除成功');
                    this.onsaveSc()
                }
            })
        }

        //上传
        onsc = (info) => {
            if (info.file.status !== 'uploading') {
            }
            if (info.file.status === 'done') {
                message.success(`${info.file.name} 上传成功`);
                this.onsaveSc()
    
    
            } else if (info.file.status === 'error') {
                message.error(`${info.file.name} 上传失败.`);
            }
        }

            //查询上传附件
    onsaveSc = () => {
        request({
            url: `/zhrq-web/gas/company/getFileUploadList?id=${PublicService.getCookie('pid')}`,
            method: 'GET',
        }).then(data => {
            console.log(data)
            if (data.rc == 0) {
                if (data.ret.length > 0) {
                    data.ret.forEach((item, index) => {
                        item.createTime = moment(item.createTime).format("YYYY-MM-DD HH:mm:ss")
                    })
                }
                this.setState({
                    dataSource: data.ret
                })
            }
        })
    }





    // modal保存
    onModalSave = () => {
        this.props.form.validateFieldsAndScroll((err, val) => {
            if (!err) {
                this.props.onModalSave(val, this.props.type)
            }
        });
    };
    // 筛选条件组件change事件
    getChangeValue(value, e) {
        let t = this;
        let params = {};
        params[value] = e.target.value;
        t.setState({ params });
    }
    onCheckbox(value, e) {
        let t = this;
        let params = {};
        params[value] = e.target.value;
        t.setState({ params });
    }
    render() {
        let t = this;
        let { getFieldDecorator } = t.props.form;

        console.log(this.props.fileUploadList)

        let items = this.props.data
        const props1 = {
            name: 'file',
            action: '/zhrq-web/common/fileupload/dopost',

            headers: {
                authorization: 'authorization-text',
            },
            data: {
                pid: PublicService.getCookie('pid'),
                tableName: "t_gas_company",
                uploadUser: "系统管理员",
            },
            multiple: true,
            showUploadList: false

        };
        return (
            <div>
                <MyModal
                    {...t.props}

                    width={600}
                    onModalSave={t.onModalSave}>
                    <Form>
                        <Row gutter={48}>
                            {
                                items.map((item, index) => {
                                    if (item.type === 'input') {
                                        return (
                                            <Col key={index} span={item.span || 24} offset={item.offset}>
                                                <FormItem colon={false} label={item.label}>
                                                    {
                                                        getFieldDecorator(item.paramName, {
                                                            initialValue: item.initialValue || "",
                                                            rules: item.rules || null,
                                                        })(
                                                            <Input disabled={false} addonAfter={item.text} placeholder={'请输入'} style={{ width: item.width }} size="small"
                                                                onChange={item.onChange} />
                                                        )
                                                    }
                                                </FormItem>
                                            </Col>
                                        )
                                    }
                                    if (item.type === 'Upload') {
                                        return (
                                            <Col key={index} span={item.span || 24} offset={item.offset}>
                                                <FormItem colon={false} label={item.label} >
                                                    {
                                                        getFieldDecorator(item.paramName, {
                                                            initialValue: item.initialValue || "",
                                                            rules: item.rules || null,
                                                        })(

                                                            <Upload {...props1} onChange={this.onsc}>
                                                                <Button>
                                                                    <Icon type="upload" /> 上传文件
                                                            </Button>
                                                            </Upload>

                                                        )
                                                    }
                                                </FormItem>
                                            </Col>
                                        )
                                    }
                                })
                            }

                        </Row>

                    </Form>
                    <MyTable
                        pagination={false}
                        columns={this.state.columns}
                        // scroll={{ y: windowWidth < 1400 ? height - 280 : height - 300 }}
                        dataSource={PublicService.transformArrayData(this.state.dataSource, true, true)}
                    />
                </MyModal>

            </div>
        )
    }
}
export default Form.create()(CheckModel);
